﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.Common;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.Payment
{
    public partial class LstPayment : AdminUserControl
    {

        #region Khai báo biến
        private int totalRecords;
        private int cPage = 1;
        private int page;
        private string keyword;
        private int checkType;
        private int ZoneId;
        private DateTime dateTo;
        private DateTime dateFrom;
        #endregion

        #region OnInit
        protected override void OnInit(EventArgs e)
        {
            try
            {
                base.OnInit(e);
                if (Request.QueryString["keyword"] != null || Request.QueryString["keyword"] != "")
                    txtKeysearch.Text = Request.QueryString["keyword"];

                if (Request.QueryString["dateFrom"] != null && Request.QueryString["dateFrom"] != "")
                {
                    dateFrom = Convert.ToDateTime(Request.QueryString["dateFrom"].Trim());
                    txtDatefrom.Text = dateFrom.ToString("MM/dd/yyyy");
                }
                else
                {
                    dateFrom = DateTime.Now.AddMonths(-2);
                    txtDatefrom.Text = dateFrom.ToString("MM/dd/yyyy");
                }

                if (Request.QueryString["dateTo"] != null && Request.QueryString["dateTo"] != "")
                {
                    dateTo = Convert.ToDateTime(Request.QueryString["dateTo"].Trim());
                    txtDateTo.Text = dateTo.ToString("MM/dd/yyyy");
                }
                else
                {
                    dateTo = DateTime.Now.AddMonths(+1);
                    txtDateTo.Text = dateTo.ToString("MM/dd/yyyy");
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = "Error: " + ex.Message;
            }
        }
        #endregion

        #region OnPreRender
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                page = int.Parse("0" + Request.QueryString["page"]);
                if (page == 0) page = 1;
                base.OnPreRender(e);
                LoadData();
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

        }
        #endregion

       #region LoadData
        private void LoadData()
        {

            totalRecords = PaymentBL.SelectCount(txtKeysearch.Text, DateTime.Parse(txtDatefrom.Text), DateTime.Parse(txtDateTo.Text).AddDays(+1));
            lbTotal.Text = "Tổng số bản ghi: " + totalRecords;
            DataTable dt;
            if (Request.QueryString.Get("cpage") != null)
                cPage = ObjConvertUtility.ToInt32(Request.QueryString.Get("cPage"));
            if (cPage == 0) cPage = 1;
            int from = (cPage - 1) * Const.pageSize20 + 1;
            if (from == 0) from = 1;
            int to = (cPage) * Const.pageSize20;

            PagingAjax1.totalRecords = totalRecords;
            PagingAjax1.numPerPage = Const.pageSize20;
            PagingAjax1.queryParam = ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "customer_name", "from", "to", "dateFrom", "dateTo", "cPage" },
                                    new string[] { txtKeysearch.Text, from.ToString(), to.ToString(), dateFrom.ToString(), dateTo.ToString(), "" });

            PagingAjax1.defaultQueryParam =
                ObjMiscUtility.GetCPPage(CurrentCommand.CtrlKey,
                                    new string[] { "customer_name", "from", "to" },
                                    new string[] { txtKeysearch.Text, from.ToString(), to.ToString() });

            string order = null;
            dt = PaymentBL.Search(txtKeysearch.Text, DateTime.Parse(txtDatefrom.Text), DateTime.Parse(txtDateTo.Text).AddDays(+1), from, to, order);
            if (dt != null && dt.Rows.Count > 0)
            {

                grdPayment.DataSource = dt;
                grdPayment.RowDataBound += new GridViewRowEventHandler(grdPayment_RowDataBound);
                grdPayment.DataBind();

            }
        }
        #endregion


        #region RowDataBound
        protected void grdPayment_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
              e.Row.Cells[0].Text = (Const.pageSize30 * (cPage - 1) + e.Row.DataItemIndex + 1).ToString();
               
            }
        }

        #endregion
        

      
        #region tìm kiếm
        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            Response.Redirect("Default.aspx?ctrlkey=" + CurrentCommand.CtrlKey + "&keyword=" + txtKeysearch.Text.Trim() + "&dateFrom=" + txtDatefrom.Text.Trim() + "&dateTo=" + txtDateTo.Text.Trim() + "&page=" + page);

        }
        #endregion

      
        protected void grdPayment_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                switch (e.CommandName)
                {
                    case "Commanddelete":
                        try
                        {
                            int id = Convert.ToInt32(e.CommandArgument);
                            PaymentBL.Delete(id);
                            LoadData();
                        }
                        catch (Exception ex)
                        {
                            lblMessage.Text = ex.Message;
                        }
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }
        }
       
    }
}